package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.R;
import com.example.reggie.domain.Employee;
import com.example.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController extends BaseController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee) {
        //1.将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据页面提交的用户名username进行查询数据库
        LambdaQueryWrapper<Employee> lq = new LambdaQueryWrapper<Employee>();
        lq.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lq);
        //3.如果没有查询到则返回登陆失败结果
        if (emp == null) {
            return R.error("登陆失败！");
        }
        //4.密码比对，如果不一致则返回登陆是啊比结果
        if (!emp.getPassword().equals(password)) {
            return R.error("登陆失败！");
        }
        //5.查看员工状态，如果为禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("用户已禁用！");
        }
        //6.登陆成功，将员工id存入session并返回登陆成功结果
        session.setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout() {
        session.removeAttribute("employee");
        return R.success("退出成功！");
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee) {
        log.info("用户信息：{}", employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        Long id = (Long) session.getAttribute("employee");
//        employee.setUpdateUser(id);
//        employee.setCreateUser(id);
        employeeService.save(employee);
        return R.success("添加用戶成功！");
    }

    /**
     * 员工分页信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageEmployee(Integer page, Integer pageSize, String name) {
        log.info("page: {},pageSize: {},name: {}", page, pageSize, name);
        Page<Employee> pageInfo = new Page<Employee>(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        employeeService.page(pageInfo, lqw);
        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     *
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Employee employee) {


//        employee.setUpdateUser((Long) session.getAttribute("employee"));
//        employee.setUpdateTime(LocalDateTime.now());
        boolean update = employeeService.updateById(employee);
        long id = Thread.currentThread().getId();
        log.info("ThreadLocal ID:{}",id);
        if (!update) {
            return R.error("更新状态失败！");
        }
        return R.success("更新成功！");
    }

    /**
     * 根据id查询员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getEmployeeById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }else {
            return R.error("没有查询成功！");
        }
    }




}
